<template>
  <view class="my-settle-container">
    <view class="radio">
      <!-- 全选 -->
      <radio
        :checked="selectAll"
        color="#C00000"
        @click="checked(!selectAll)"
      />
    </view>
    <view>
      合计：<text class="amount">￥{{ amount }}</text>
    </view>
    <view class="btn-settle" @click="settle">结算({{ count }})</view>
  </view>
</template>

<script>
import { orderCreateApi } from '@/api/index'
import { mapGetters, mapMutations } from 'vuex'
export default {
  data () {
    return {
      time: 3,
      timer: null
    };
  },


  computed: {
    ...mapGetters('cart', ['amount', 'selectAll', 'count', 'goodsInfo']),
    ...mapGetters('user', ['getAddress', 'getToken', 'address'])
  },

  methods: {
    ...mapMutations('cart', ['checkedAll']),
    ...mapMutations('user', ['setRedirectInfo']),
    checked (value) {
      this.checkedAll(value)
    },

    settle () {
      if (this.count === 0) {
        return uni.showToast({
          title: '请选择商品结算',
          duration: 800,
          icon: 'error',
          mask: true
        })
      }
      // if (!this.getAddress) {
      //   return uni.showToast({
      //     title: '请选择收货地址',
      //     duration: 800,
      //     icon: 'error',
      //     mask: true
      //   })
      // }

      // 判断是否登录
      if (!this.getToken) {
        uni.showToast({
          title: `您还未登录！${this.time}秒后自动跳转到登录页`,
          duration: 1000,
          icon: 'none',
          mask: true
        })

        this.timer = setInterval(() => {
          if (this.time <= 1) {
            this.time = 3

            uni.switchTab({
              url: '/pages/my/index',
              success: () => {
                this.setRedirectInfo('/pages/cart/index')
              }
            })
            clearInterval(this.timer)
            return
          }

          this.time--


          // 判断是否登录
          if (!this.getToken) {
            uni.showToast({
              title: `您还未登录！${this.time}秒后自动跳转到登录页`,
              duration: 1000,
              icon: 'none',
              mask: true
            })
          }
        }, 1000);
      }

      this.orderPay()
    },

    // 下单结算
    async orderPay () {
      // 下单
      // 准备下单所要的参数
      const params = {
        order_price: this.amount,
        consignee_addr: `${this.address} ${this.getAddress.detailInfo}  ${this.getAddress.telNumber}`,
        goods: this.goodsInfo.filter(item => item.goods_state).map(item => {
          return {
            goods_id: item.goods_id,
            goods_number: item.goods_number,
            goods_price: item.goods_price
          }
        })
      }

      const res = await orderCreateApi(params)
      console.log(res)

      // 此处因为没有token，故假装有token，为了项目后续进行
      const order_number = 123456789123456789
    }


  },



}
</script>

<style lang="scss" scoped>
.my-settle-container {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  // 将背景色从 cyan 改为 white
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 5px;
  font-size: 14px;
  border-top: 1px solid #efefef;

  .radio {
    display: flex;
    align-items: center;
  }

  .amount {
    color: #c00000;
  }

  .btn-settle {
    height: 50px;
    min-width: 100px;
    background-color: #c00000;
    color: white;
    line-height: 50px;
    text-align: center;
    padding: 0 10px;
  }
}
</style>
